
package com.sm.shoplook.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.customer.Customer;
import com.sm.shoplook.service.customer.CustomerService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.system.custom.CustomPageSearcheReqVo;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);
	@Autowired
	private CustomerService customerService;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String viewCustomer() {
		return "/customer/customer_manager";
	}

	@ResponseBody
	@RequestMapping(value = "/getAllCustomListPage", method = RequestMethod.POST)
	public ApiResult<Map<String, Object>> getAllCustomListPage(CustomPageSearcheReqVo customPageSearcheReqVo) {
		HashMap<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<Customer>> apiResult = customerService.getAllCustomListPage(customPageSearcheReqVo);
		PageInfo<Customer> pageInfo = apiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	@RequestMapping(value = "/info_customer/{id}", method = RequestMethod.GET)
	public String infoCustomer(@PathVariable(value = "id") Integer id, Model model) {
		ApiResult<Customer> customerApiResult = customerService.selectCustomerById(id);
		Customer customer = customerApiResult.getData();
		if (customer == null) {
		    customer = new Customer();
		}
		model.addAttribute("customer", customer);
		return "/customer/info_customer";
	}

	@ResponseBody
	@RequestMapping(value = "/auditCustomer", method = RequestMethod.POST)
	public ApiResult updateCustomer(Customer customer) {
		try {
            customer.setUpdateTime(new Date());
			customerService.updateCustomer(customer);
		} catch (ApiException e) {
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}
}
